package com.mbc.web.common.server.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mbc.web.common.server.SessionConstants;
import com.mbc.web.common.shared.HttpStatus;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRPdfExporterParameter;

@SuppressWarnings("serial")
public class PrintServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("application/pdf");
		JasperPrint jasperPrint=(JasperPrint)req.getSession().getAttribute(SessionConstants.REPORT_DATA);
		JRPdfExporter jrPDFExporter = new JRPdfExporter();
		jrPDFExporter.setParameter(JRPdfExporterParameter.JASPER_PRINT, jasperPrint);
		jrPDFExporter.setParameter(JRPdfExporterParameter.PDF_JAVASCRIPT,"this.print({bUI: false,bSilent: true});");
		jrPDFExporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM,resp.getOutputStream());
		try {
			jrPDFExporter.exportReport();
		} catch (JRException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resp.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		finally{
			req.getSession().removeAttribute(SessionConstants.REPORT_DATA);
		}
	}
}
